/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * bzr_anwendung.java
 *
 * Created on 05.05.2011, 15:16:42
 */
package Anwendung;

import bzr_veranstaltungverwaltung.Mitarbeiter;
import bzr_veranstaltungverwaltung.Veranstaltungen;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.awt.Dimension;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Flo
 */
public class bzr_anwendung extends javax.swing.JFrame {

    /** Creates new form bzr_anwendung */
    public Connection con;
    public DefaultListModel dlm_va;
    public DefaultListModel dlm_ma;

    public bzr_anwendung() {
        verbindenDB();
        Dimension screenSize = this.getToolkit().getScreenSize();
        this.setPreferredSize(screenSize);

        initComponents();
        initVAListe();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList_Veranstaltungen = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jSpinner_Zeitspanne = new javax.swing.JSpinner();
        jButton_NeueVeranstaltung = new javax.swing.JButton();
        jLabel_VA_Name = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListMitarbeiter = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jListWerbemittel = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jListAnsprechperson = new javax.swing.JList();
        jButtonInfosverschicken = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Veranstaltungsverwaltung");
        setAlwaysOnTop(true);

        jList_Veranstaltungen.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList_Veranstaltungen.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList_VeranstaltungenValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList_Veranstaltungen);

        jLabel1.setText("Zeitspanne:");

        jSpinner_Zeitspanne.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), new java.util.Date(1304694744112L), null, java.util.Calendar.DAY_OF_MONTH));

        jButton_NeueVeranstaltung.setText("Neue Veranstaltung");
        jButton_NeueVeranstaltung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_NeueVeranstaltungActionPerformed(evt);
            }
        });

        jLabel_VA_Name.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel_VA_Name.setText("Veranstaltung");

        jLabel2.setText("Mitarbeiter:");

        jScrollPane2.setViewportView(jListMitarbeiter);

        jLabel3.setText("Werbemittel:");

        jScrollPane3.setViewportView(jListWerbemittel);

        jLabel4.setText("Ansprechperson:");

        jScrollPane4.setViewportView(jListAnsprechperson);

        jButtonInfosverschicken.setText("Infos verschicken");

        jLabel5.setText("Info:");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane5.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jSpinner_Zeitspanne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton_NeueVeranstaltung, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addComponent(jScrollPane4)))
                        .addGap(180, 180, 180)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(44, 44, 44)
                        .addComponent(jButtonInfosverschicken))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(528, 528, 528)
                        .addComponent(jLabel_VA_Name, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel_VA_Name, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                                    .addContainerGap())
                                .addComponent(jLabel5))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(jSpinner_Zeitspanne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(jButton_NeueVeranstaltung, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                                .addComponent(jButtonInfosverschicken)
                                .addGap(23, 23, 23))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                                .addContainerGap())))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jList_VeranstaltungenValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList_VeranstaltungenValueChanged
        // TODO add your handling code here:
        if(jList_Veranstaltungen.getSelectedIndex()>0){
        Veranstaltungen va = (Veranstaltungen) jList_Veranstaltungen.getSelectedValue();
        jLabel_VA_Name.setText(va.getName());
        initMA_List();
        }
    }//GEN-LAST:event_jList_VeranstaltungenValueChanged

    private void jButton_NeueVeranstaltungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_NeueVeranstaltungActionPerformed
        // TODO add your handling code here:
        bzr_neueVeranstaltung nva = new bzr_neueVeranstaltung();
        nva.setVisible(true);
    }//GEN-LAST:event_jButton_NeueVeranstaltungActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new bzr_anwendung().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonInfosverschicken;
    private javax.swing.JButton jButton_NeueVeranstaltung;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel_VA_Name;
    private javax.swing.JList jListAnsprechperson;
    private javax.swing.JList jListMitarbeiter;
    private javax.swing.JList jListWerbemittel;
    private javax.swing.JList jList_Veranstaltungen;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JSpinner jSpinner_Zeitspanne;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

    private void verbindenDB() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://mysql.bezirksrundschau.com:3306/brdbm_1";
            con = (Connection) DriverManager.getConnection(url, "brdbm", "br2011!db");
        } catch (Exception ex) {
            Logger.getLogger(bzr_anwendung.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void initVAListe() {
        try {
            dlm_va = new DefaultListModel();
            Statement st = (Statement) con.createStatement();
            ResultSet rs = (ResultSet) st.executeQuery("Select * FROM Veranstaltungen");
            while (rs.next()) {
                Veranstaltungen va = new Veranstaltungen();
                va.setName(rs.getString("Name"));
                va.setOrt(rs.getString("Ort"));
                va.setPlz(rs.getInt("PLZ"));
                va.setStraße(rs.getString("Strasse"));
                va.setHausnr(rs.getInt("HausNr"));
                va.setStartdatum(rs.getDate("Startdatum"));
                va.setEnddatum(rs.getDate("Enddatum"));
                va.setZustaendigeGeschaeftsstelle(rs.getString("ZustaendigeGeschaeftsstelle"));
                va.setV_id(rs.getInt("Veranstaltungs_id"));
                dlm_va.addElement(va);
            }
            jList_Veranstaltungen.setModel(dlm_va);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, ex.toString());
        }

    }

    private void initMA_List() {
        try {
            dlm_ma = new DefaultListModel();
            Veranstaltungen va = (Veranstaltungen) jList_Veranstaltungen.getSelectedValue();
            Statement s = (Statement) con.createStatement();
            ResultSet rs = (ResultSet) s.executeQuery("SELECT * FROM VA_MA WHERE Veranstaltungen_VA_ID=" + va.getV_id());
            while (rs.next()) {
                int i = (rs.getInt("Freie_Mitarbeiter_MA_ID"));
                Statement s2 = (Statement) con.createStatement();
                ResultSet rs2 = (ResultSet) s2.executeQuery("SELECT * FROM Freie_Mitarbeiter WHERE Mitarbeiter_ID=" + i);
                while (rs2.next()) {
                    Mitarbeiter ma = new Mitarbeiter();
                    ma.setVorname(rs2.getString("Vorname"));
                    ma.setNachname(rs2.getString("Nachname"));
                    ma.setTelefonnummer(rs2.getInt("Telefonnummer"));
                    ma.setEmail(rs2.getString("Email"));
                    ma.setBezirk(rs2.getString("Bezirk"));
                    ma.setGebDatum(rs2.getDate("GebDatum"));
                    ma.setAusbildung(rs2.getString("Ausbildung"));
                    ma.setKonfektionsgröße(rs2.getInt("Konfektionsgroesse"));
                    ma.setFuehrerschein(rs2.getString("Fuehrerschein"));
                    ma.setSvnr(rs2.getInt("SVNr"));
                    ma.setStaatsbuergerschaft(rs2.getString("Staatsbuergerschaft"));
                    ma.setBankverbindung(rs2.getInt("Bankverbindung"));
                    ma.setMaart(rs2.getString("MAArt"));
                    dlm_ma.addElement(ma);

                }
                jListMitarbeiter.setModel(dlm_ma);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, ex.toString());
        }
    }
}
